function h1 = fm_busfig(varargin)
% FM_BUSFIG graphical user interface for custom bus selections
%
%see also the function FM_EQUIV and FM_EQUIVFIG
%
%Author:    Federico Milano
%Update:    02-Apr-2008
%Version:   0.1
%
%E-mail:    federico.milano@ucd.ie
%Web-site:  faraday1.ucd.ie/psat.html
%
% Copyright (C) 2002-2019 Federico Milano

global Theme EQUIV Bus Areas Regions File Path

switch nargin
 case 0, flag = 'null';
 case 1, flag = varargin{1};
end

switch flag

 case 'writelist'

  hdl_bus = findobj(gcf,'Tag','BusList');
  idx = get(hdl_bus,'Value');
  EQUIV.buslist = idx;
  listfile = [strrep(File.data,'(mdl)',''),'.lst'];
  [fid,message] = fopen([Path.data,listfile],'wt');
  if fid == -1
    fm_disp(message)
    return
  end
  for i = 1:length(idx)
    count = fprintf(fid,'%s\n',Bus.names{idx(i)});
  end
  fclose(fid);
  fm_disp(['File <',listfile,'> written in data folder.'])
  close(gcf)
  return

 case 'area'

  hdl_area = findobj(gcf,'Tag','PopUpArea');
  hdl_vol = findobj(gcf,'Tag','PopUpVoltage');
  hdl_bus = findobj(gcf,'Tag','BusList');
  busdata = get(hdl_bus,'UserData');
  value = get(hdl_area,'Value');
  idx = [];
  for i = 1:length(value)
    idx = [idx; find(getarea(Bus,0,0) == value(i))];
  end
  set(hdl_bus,'Value',unique(idx),'ListboxTop',idx(1));
  set(hdl_vol,'Value',[])
  return

 case 'region'

  hdl_region = findobj(gcf,'Tag','PopUpRegion');
  hdl_vol = findobj(gcf,'Tag','PopUpVoltage');
  hdl_bus = findobj(gcf,'Tag','BusList');
  busdata = get(hdl_bus,'UserData');
  value = get(hdl_region,'Value');
  idx = [];
  for i = 1:length(value)
    idx = [idx; find(getregion(Bus,0,0) == value(i))];
  end
  set(hdl_bus,'Value',unique(idx),'ListboxTop',idx(1));
  set(hdl_vol,'Value',[])
  return

 case 'voltage'

  hdl_vol = findobj(gcf,'Tag','PopUpVoltage');
  hdl_bus = findobj(gcf,'Tag','BusList');
  hdl_area = findobj(gcf,'Tag','PopUpArea');
  busdata = get(hdl_bus,'UserData');
  voltages = get(hdl_vol,'String');
  value = get(hdl_vol,'Value');
  idx = [];
  for i = 1:length(value)
    idx = [idx; find(getkv(Bus,0,0) == str2num(voltages{value(i)}))];
  end
  set(hdl_bus,'Value',unique(idx),'ListboxTop',idx(1));
  set(hdl_area,'Value',[])
  return

end

h1 = figure(...
    'Units','normalized',...
    'Color',Theme.color01, ...
    'Colormap',[], ...
    'MenuBar','none',...
    'Name','Bus Selection',...
    'NumberTitle','off',...
    'PaperPosition',[18 180 576 432], ...
    'Position',sizefig(0.525,0.55),...
    'Resize','on',...
    'WindowStyle','modal', ...
    'FileName','fm_busfig',...
    'CreateFcn','Fig.bus = gcf;', ...
    'DeleteFcn','Fig.bus = -1;');
fm_set colormap

% Menu File/Open
hm = uimenu('Parent',h1, ...
            'Label','File', ...
            'Tag','MenuFile');

hs = uimenu('Parent',hm, ...
            'Callback','close(gcf)', ...
            'Label', 'Exit', ...
            'Tag','OpenLF', ...
            'Separator','on', ...
            'Accelerator','q');

h2 = uipanel(...
    'Parent',h1,...
    'Units','normalized',...
    'FontSize',10,...
    'BackgroundColor', Theme.color01, ...
    'Title',{  'Bus Selection' },...
    'Tag','uipanel1',...
    'Clipping','on',...
    'Position',[0.1 0.1 0.35 0.8]);

h8 = uibuttongroup(...
    'Parent',h1,...
    'Units','normalized',...
    'FontSize',10,...
    'BackgroundColor', Theme.color01, ...
    'Title',{  'Advanced Settings' },...
    'Tag','uipanel2',...
    'Clipping','on',...
    'Position',[0.55 0.1 0.35 0.8]);

ht = uicontrol( ...
    'Parent',h2, ...
    'Units','normalized', ...
    'BackgroundColor', Theme.color01, ...
    'Enable', 'inactive', ...
    'HorizontalAlignment','left', ...
    'Position',[0.1 0.95 0.8 0.05], ...
    'String','Bus Names', ...
    'Style','text', ...
    'Tag','TextBus');

ht = uicontrol( ...
    'Parent',h2, ...
    'Units','normalized', ...
    'BackgroundColor',Theme.color04, ...
    'ForegroundColor',Theme.color05, ...
    'HorizontalAlignment','left', ...
    'Position',[0.1 0.45 0.8 0.5], ...
    'Max',20, ...
    'String',Bus.names, ...
    'Style','listbox', ...
    'Value',[], ...
    'Tag','BusList');

% -----------------------------------------------------------------
% Voltage Rate
% -----------------------------------------------------------------

ht = uicontrol( ...
    'Parent',h2, ...
    'Units','normalized', ...
    'BackgroundColor', Theme.color01, ...
    'Enable', 'inactive', ...
    'HorizontalAlignment','left', ...
    'Position',[0.1 0.35 0.8 0.05], ...
    'String','Select Voltage Rate', ...
    'Style','text', ...
    'Tag','TextVoltage');

voltages = unique(getkv(Bus,0,0));
volnames = cell(length(voltages),1);
for i = 1:length(voltages)
  volnames{i} = num2str(voltages(i));
end

h13 = uicontrol(...
    'Parent',h2,...
    'Units','normalized',...
    'HorizontalAlignment','center', ...
    'BackgroundColor',Theme.color04, ...
    'ForegroundColor',Theme.color05, ...
    'FontName','Curier', ...
    'Callback','fm_busfig voltage',...
    'FontSize',10,...
    'Position',[0.1 0.05 0.8 0.3],...
    'String',volnames,...
    'Max',20,...
    'Style','listbox',...
    'Value',[], ...
    'Tag','PopUpVoltage');

% -----------------------------------------------------------------
% Area
% -----------------------------------------------------------------

ht = uicontrol( ...
    'Parent',h8, ...
    'Units','normalized', ...
    'BackgroundColor', Theme.color01, ...
    'Enable', 'inactive', ...
    'HorizontalAlignment','left', ...
    'Position',[0.1 0.9 0.8 0.05], ...
    'String','Select Area', ...
    'Style','text', ...
    'Tag','TextArea');

h13 = uicontrol(...
    'Parent',h8,...
    'Units','normalized',...
    'HorizontalAlignment','center', ...
    'BackgroundColor',Theme.color04, ...
    'ForegroundColor',Theme.color05, ...
    'FontName','Curier', ...
    'Callback','fm_busfig area',...
    'FontSize',10,...
    'Position',[0.1 0.6 0.8 0.3],...
    'String',Areas.names,...
    'Max',20,...
    'Style','listbox',...
    'Tag','PopUpArea',...
    'Value',[]);

% -----------------------------------------------------------------
% Region
% -----------------------------------------------------------------

ht = uicontrol( ...
    'Parent',h8, ...
    'Units','normalized', ...
    'BackgroundColor', Theme.color01, ...
    'Enable', 'inactive', ...
    'HorizontalAlignment','left', ...
    'Position',[0.1 0.5 0.8 0.05], ...
    'String','Select Region', ...
    'Style','text', ...
    'Tag','TextRegion');

h13 = uicontrol(...
    'Parent',h8,...
    'Units','normalized',...
    'HorizontalAlignment','center', ...
    'BackgroundColor',Theme.color04, ...
    'ForegroundColor',Theme.color05, ...
    'FontName','Curier', ...
    'Callback','fm_busfig region',...
    'FontSize',10,...
    'Position',[0.1 0.2 0.8 0.3],...
    'String',Regions.names,...
    'Max',20,...
    'Style','listbox',...
    'Tag','PopUpRegion',...
    'Value',[]);

% -----------------------------------------------------------------
% Buttons
% -----------------------------------------------------------------

h13 = uicontrol(...
    'Parent',h8,...
    'Units','normalized',...
    'HorizontalAlignment','center', ...
    'BackgroundColor',Theme.color01,...
    'FontName','Curier', ...
    'Callback','fm_busfig writelist',...
    'FontSize',10,...
    'Position',[0.1 0.05 0.35 0.1],...
    'String','Save',...
    'Tag','ButtonList');

h13 = uicontrol(...
    'Parent',h8,...
    'Units','normalized',...
    'HorizontalAlignment','center', ...
    'BackgroundColor',Theme.color01,...
    'FontName','Curier', ...
    'Callback','close(gcf)',...
    'FontSize',10,...
    'Position',[0.55 0.05 0.35 0.1],...
    'String','Cancel',...
    'Tag','ButtonClose');
